package com.lt.bcra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lt.bcra.mapper.BcraPersonalFactorsMapper;
import com.lt.bcra.entity.BcraPersonalFactors;
import com.lt.bcra.exception.BaseException;
import com.lt.bcra.service.BcraPersonalFactorsService;
import com.lt.bcra.result.ResponseEnum;
import com.lt.bcra.result.ResultData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lt.bcra.handler.AgeCalculation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;


/**
 * <p>
 * 个人因素表 服务实现类
 * </p>
 *
 * @author LuoTian
 * @since 2022-05-25
 */
@Service
public class BcraPersonalFactorsServiceImpl extends ServiceImpl<BcraPersonalFactorsMapper, BcraPersonalFactors> implements BcraPersonalFactorsService {

    @Autowired
    private BcraPersonalFactorsMapper bcraPersonalFactorsMapper;


    /**
     * 添加个人因素表
     *
     * @param bcraPersonalFactors
     * @return
     */
    @Override
    public ResultData saveModule(BcraPersonalFactors bcraPersonalFactors) {

        calculation(bcraPersonalFactors);

        bcraPersonalFactors.setModuleTitle("个人因素表");
        bcraPersonalFactors.setModuleId(1);

        int ok = bcraPersonalFactorsMapper.insert(bcraPersonalFactors);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.CREATE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData updateModule(BcraPersonalFactors bcraPersonalFactors) {
        calculation(bcraPersonalFactors);

        QueryWrapper<BcraPersonalFactors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", bcraPersonalFactors.getQuestionnaireCode());
        int ok = bcraPersonalFactorsMapper.update(bcraPersonalFactors, queryWrapper);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.UPDATE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData removeByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraPersonalFactors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        int ok = bcraPersonalFactorsMapper.delete(queryWrapper);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.DELETE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData selectByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraPersonalFactors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        BcraPersonalFactors bcraPersonalFactors = bcraPersonalFactorsMapper.selectOne(queryWrapper);
        if (bcraPersonalFactors == null) {
            throw new BaseException(ResponseEnum.SELECT_DATA_ERROR);
        }
        return ResultData.getResult(bcraPersonalFactors);
    }

    /**
     * 计算bmi和年龄
     *
     * @param bcraPersonalFactors
     */
    public void calculation(BcraPersonalFactors bcraPersonalFactors) {
        //bmi自动计算
        if (!StringUtils.isEmpty(bcraPersonalFactors.getWeight()) && !StringUtils.isEmpty(bcraPersonalFactors.getHeight())) {
            if (StringUtils.isEmpty(bcraPersonalFactors.getBmi())) {
                Double height = bcraPersonalFactors.getHeight();
                Double weight = bcraPersonalFactors.getWeight();
                Double bmiBefore = weight / (Math.pow(height, 2));
                //保留两位小数
                double bmi = Double.parseDouble(String.format("%.2f", bmiBefore));
                bcraPersonalFactors.setBmi(bmi);
            }
        }
        //如果没有填写年龄，根据日期计算年龄
        if (!StringUtils.isEmpty(bcraPersonalFactors.getBirthday())) {
            if (StringUtils.isEmpty(bcraPersonalFactors.getAge())) {
                Date date = AgeCalculation.transformation(bcraPersonalFactors.getBirthday());
                int age = 0;
                try {
                    age = AgeCalculation.getAge(date);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                bcraPersonalFactors.setAge(age);
            }
        }
    }
}
